﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Twitterizer;
using System.Net;
using System.IO;

namespace Twillo.Core.Controls
{
    public class UserTweetList : Panel
    {
        [Category("Twitter")]
        public TwitterStatusCollection Items { get { return _items; } set { _items = value; RefreshControl(); } }
        [Category("Events")]
        public event LinkClickedHandler LinkClicked;
        public delegate void LinkClickedHandler(object sender, TwitterStatusLabelClickEventArgs e);

        private TwitterStatusCollection _items = null;

        private void RefreshControl()
        {
            this.Controls.Clear();
            this.AutoScroll = true;

            if (DesignMode) { return; }
            if (_items == null) { return; }

            TableLayoutPanel ctl = new TableLayoutPanel();
            ctl.AutoSize = true;
            ctl.RowCount = 0;
            ctl.ColumnCount = 1;
            ctl.RowStyles.Clear();
            ctl.RowCount = _items.Count;
            ctl.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
            ctl.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
            int rows = 0;
            foreach (TwitterStatus state in _items)
            {
                RowStyle style = new RowStyle(SizeType.AutoSize);
                ctl.RowStyles.Add(style);
                UserTweetBanner b = new UserTweetBanner();
                b.Size = new System.Drawing.Size(this.Width, 80);
                b.LinkClicked += new UserTweetBanner.LinkClickedHandler(UserTweetBanner_LinkClicked);
                b.BackColor = this.BackColor;
                b.TwitterStatus = state;
                b.Dock = DockStyle.Fill;
                ctl.Controls.Add(b, 1, rows);
                rows += 1;
            }

            this.Controls.Add(ctl);
        }


        protected void UserTweetBanner_LinkClicked(object sender, TwitterStatusLabelClickEventArgs e)
        {
            if (LinkClicked != null) { LinkClicked(this, e); }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // UserTweetList
            // 
            this.ResumeLayout(false);

        }

        public void Repaint(Int32 Width)
        {
            this.Controls[0].Width = this.Width;
        }
    }
}
